package com.cmw.socket.core;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Remark IO 参数类
 * @author chengmingwei
 * @date 2020-06-22 10:05
 */
public class IoArgs {
    byte[] byteBuffer = new byte[256];
    ByteBuffer buffer = ByteBuffer.wrap(byteBuffer);

    public int read(SocketChannel channel) throws IOException {
        buffer.clear();
        return channel.read(buffer);
    }

    public int write(SocketChannel channel) throws IOException {
        return channel.write(buffer);
    }

    public String bufferString(){
        return new String(byteBuffer, 0, buffer.position() - 1); // 减1，丢弃换行符
    }

    public interface  IoArgsEventListener{
        void onStarted(IoArgs args);

        void onCompleted(IoArgs args);
    }
}
